package subject.tengxun.middle;

import java.util.Arrays;

/**
 * 功能：
 * 作者：yedou
 * 日期：2025/3/24 20:41
 */
public class rotate {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        int n = nums.length;
        int k = 3;
        k = k % n; // 处理k大于数组长度的情况

//        int[] temp = new int[n];
//        // 将后k个元素放到temp数组前面
//        System.arraycopy(nums, n - k, temp, 0, k);
//        // 将前n-k个元素放到temp数组后面
//        System.arraycopy(nums, 0, temp, k, n - k);
//        // 将temp数组复制回原数组
//        System.arraycopy(temp, 0, nums, 0, n);
//        System.out.println(Arrays.toString(temp));
//        // 反转整个数组
//        reverse(nums, 0, n - 1);
//        System.out.println(Arrays.toString(nums));
//        // 反转前k个元素
//        reverse(nums, 0, k - 1);
//        System.out.println(Arrays.toString(nums));
//        // 反转剩余元素
//        reverse(nums, k, n - 1);
//        System.out.println(Arrays.toString(nums));

        int count = 0; // 记录已经处理了多少个元素

        for (int start = 0; count < n; start++) {
            int current = start;
            int prev = nums[start];

            do {
                int next = (current + k) % n;
                int temp = nums[next];
                nums[next] = prev;
                prev = temp;
                current = next;
                count++;
            } while (start != current);
        }
        System.out.println(Arrays.toString(nums));
    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}